package meta.api.web.data.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import meta.api.web.data.businessinfo.FolderDataInfo;
import meta.api.web.data.presentation.form.FolderInfo;

@Repository
public class DataMngDao {

    @Autowired
    private SqlSessionTemplate sqlSession;

    private static final String NAMESPACE = "meta.api.web.data.dao.dataMng.";

    public int getDataTotal(String userId, String collection, String type, String folder) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("collection", collection);
        params.put("type", type);
        params.put("folder", folder);
        return sqlSession.selectOne(NAMESPACE + "getDataTotal", params);
    }

    public List<String> getDataList(String userId, String collection, String type, String folder,
            List<String> projectionList, Boolean includeFlag,
            Integer current, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("collection", collection);
        params.put("type", type);
        params.put("folder", folder);
        params.put("projectionList", projectionList);
        params.put("includeFlag", includeFlag);
        if (current != null && pageSize != null) {
            params.put("current", pageSize * (current - 1));
            params.put("pageSize", pageSize);
        } else {
            params.put("current", null);
            params.put("pageSize", null);
        }
        return sqlSession.selectList(NAMESPACE + "getDataListByCollection", params);
    }

    public void saveData(String id, String userId, String collection, String data,
            String keyVal, String owner, String editor) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        params.put("collection", collection);
        params.put("data", data);
        params.put("keyVal", keyVal);
        params.put("owner", owner);
        params.put("editor", editor);
        sqlSession.insert(NAMESPACE + "saveData", params);
    }

    public void updateData(String id, String userId, String data, String editor) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        params.put("data", data);
        params.put("editor", editor);
        sqlSession.update(NAMESPACE + "updateDataById", params);
    }

    public String getData(String id, String userId,
            List<String> projectionList, Boolean includeFlag) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        params.put("projectionList", projectionList);
        params.put("includeFlag", includeFlag);
        return sqlSession.selectOne(NAMESPACE + "getData", params);
    }

    public void deleteData(String id, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        sqlSession.delete(NAMESPACE + "deleteData", params);
    }

    public int getFolderListTotal(String userId, String type) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        return sqlSession.selectOne(NAMESPACE + "getFolderListTotal", params);
    }

    public List<FolderInfo> getFolderList(String userId, String type) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        return sqlSession.selectList(NAMESPACE + "getFolderList", params);
    }

    public List<FolderDataInfo> getFolderDataList(String userId, String collection) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("collection", collection);
        return sqlSession.selectList(NAMESPACE + "getFolderDataList", params);
    }

    public void updateData(String userId, String collection, String folderName,
            String dataId, String data, String editor) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("collection", collection);
        params.put("folderName", folderName);
        params.put("dataId", dataId);
        params.put("data", data);
        params.put("editor", editor);
        sqlSession.update(NAMESPACE + "updateDataByCollectionAndDataId", params);
    }

    public List<String> getDataList(String userId, String collection, String folderName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("collection", collection);
        params.put("folderName", folderName);
        return sqlSession.selectList(NAMESPACE + "getDataListByCollectionAndFolderName", params);
    }
}
